package net.sky.report.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Dao {    
				         			
    public final static String DRIVER = "oracle.jdbc.driver.OracleDriver"; 
   
    public final static String URL    = "jdbc:oracle:thin:@192.168.100.21:1521:asms";
    public final static String DBNAME = "sky";                                                          
    public final static String DBPASS = "sky";   
    
//    public final static String URL    = "jdbc:oracle:thin:@localhost:1521:sky";
//    public final static String DBNAME = "asms";                                                          
//    public final static String DBPASS = "asms"; 
    
    public Connection getConn() throws ClassNotFoundException, SQLException{
        Class.forName(DRIVER);                                                    
        Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS);        
        return conn ;                                                            
    }
    
    public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {
        if(rs != null){
            try { rs.close();} catch (SQLException e) {e.printStackTrace();}
        }
        if(pstmt != null){
            try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
        }
        if(conn != null){
            try { conn.close();} catch (SQLException e) {e.printStackTrace();}
        }
    }
    /*add,delete,update*/
    public int executeSQL(String preparedSql,String[] param) {
        Connection        conn  = null;
        PreparedStatement pstmt = null;
        int               num   = 0;
        
        try {
            conn = getConn();                              
            pstmt = conn.prepareStatement(preparedSql);    
            if( param != null ) {
                for( int i = 0; i < param.length; i++ ) {
                    pstmt.setString(i+1, param[i]);         
                }
            }
            num = pstmt.executeUpdate();                    
        } catch (ClassNotFoundException e) {
            e.printStackTrace();         
        } catch (SQLException e) {
            e.printStackTrace();                            
        } finally {
            closeAll(conn,pstmt,null);
        }
        return num;
    }
    
}

